package com.tianji.remark.service.impl;//package com.tianji.remark.service.impl;
//
//import cn.hutool.core.bean.BeanUtil;
//import cn.hutool.core.util.StrUtil;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.tianji.api.dto.remark.LikedTimesDTO;
//import com.tianji.common.constants.MqConstants;
//import com.tianji.common.utils.UserContext;
//import com.tianji.remark.domain.dto.LikeRecordFormDTO;
//import com.tianji.remark.domain.po.LikedRecord;
//import com.tianji.remark.mapper.LikedRecordMapper;
//import com.tianji.remark.service.ILikedRecordService;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//import java.util.Set;
//import java.util.stream.Collectors;
//
///**
// * <p>
// * 点赞记录表 服务实现类
// * </p>
// *
// * @author Ys
// * @since 2023-08-13
// */
//@Service
//public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {
//
//
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//
//    // 点赞或取消点赞
//    @Override
//    public void addLikeRecord(LikeRecordFormDTO recordDTO) {
//        //1. 基于前端的参数，判断是执行点赞还是取消点赞
//        Boolean flag = recordDTO.getLiked() ? liked(recordDTO) : unliked(recordDTO);
//        //2. 判断是否执行成功，如果失败，则直接结束
//        if (!flag) {
//            return;
//        }
//        //3. 如果执行成功，统计点赞总数
//        Integer number = this.lambdaQuery()
//                .eq(LikedRecord::getBizId, recordDTO.getBizId())
//                .count();
//        //4. 发送MQ通知
//        rabbitTemplate.convertAndSend(
//                MqConstants.Exchange.LIKE_RECORD_EXCHANGE,
//                StrUtil.format(MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE,recordDTO.getBizType()),
//                LikedTimesDTO.of(recordDTO.getBizId(),number)
//        );
//    }
//
//
//    // 抽取处理点赞方法
//    private Boolean liked(LikeRecordFormDTO recordDTO) {
//        //1. 查询点赞记录是否存在
//        Integer number = this.lambdaQuery()
//                .eq(LikedRecord::getUserId, UserContext.getUser())
//                .eq(LikedRecord::getBizId, recordDTO.getBizId())
//                .count();
//        if (number>0) {
//            return false; // 点赞记录已存在
//        }
//        //2. 新增点赞记录
//        LikedRecord likedRecord = BeanUtil.copyProperties(recordDTO, LikedRecord.class);
//        likedRecord.setUserId(UserContext.getUser());
//        return this.save(likedRecord);
//    }
//
//    // 抽取处理取消点赞方法
//    private Boolean unliked(LikeRecordFormDTO recordDTO) {
//        // 无需判断直接删除
//        LambdaQueryWrapper<LikedRecord> qw = new LambdaQueryWrapper<>();
//        qw.eq(LikedRecord::getUserId, UserContext.getUser())
//                .eq(LikedRecord::getBizId, recordDTO.getBizId());
//        return this.remove(qw);
//    }
//
//
//    // 批量查询我的点赞状态
//    @Override
//    public Set<Long> getLikedIds(List<Long> bizIds) {
//        // 根据userId和bizIds查询
//        List<LikedRecord> recordList = this.lambdaQuery()
//                .eq(LikedRecord::getUserId, UserContext.getUser())
//                .in(LikedRecord::getBizId, bizIds)
//                .list();
//        // 取出满足条件业务id转出set集合
//        return recordList.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
//    }
//}
